package com.moji.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moji.common.utils.DateUtils;
import com.moji.entity.*;
import com.moji.mapper.MjAboutUsMapper;
import com.moji.mapper.MjProductMapper;
import com.moji.service.MjAboutUsService;
import com.moji.service.MjProductService;
import com.moji.util.XMLAppendExample;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 产品表(MjProduct)表服务实现类
 *
 * @author makejava
 * @since 2024-07-21 14:36:36
 */
@Service("mjProductService")
public class MjProductServiceImpl extends ServiceImpl<MjProductMapper, MjProduct> implements MjProductService {
    @Resource
    private MjProductMapper mjProductDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public MjProduct queryById(Long id) {
        return this.mjProductDao.selectById(id);
    }

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @Override
    public List<MjProduct> queryByPage() {
        LambdaQueryWrapper<MjProduct> queryWrapper = new LambdaQueryWrapper<>();
        List<MjProduct> page = this.list(queryWrapper);
        return page;
    }

    /**
     * 新增数据
     *
     * @param mjProduct 实例对象
     * @return 实例对象
     */
    @Override
    public MjProduct insert(MjProduct mjProduct) {
        LambdaQueryWrapper<MjProduct> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(MjProduct::getRouter,mjProduct.getRouter());
        List<MjProduct> mjProducts = mjProductDao.selectList(queryWrapper1);
        if(mjProducts!=null&&mjProducts.size()>0)
        {
            return null;
        }
        //更新sitemap.xml文件
        String url="http://47.93.153.16/product/hangkong?types="+mjProduct.getRouter();
        String date= DateUtils.formatDate(new Date(),DateUtils.DATE_FORMAT);
        XMLAppendExample.addXml(url,date,1);
        this.mjProductDao.insert(mjProduct);
        return mjProduct;
    }

    /**
     * 修改数据
     *
     * @param mjProduct 实例对象
     * @return 实例对象
     */
    @Override
    public MjProduct update(MjProduct mjProduct) {
        LambdaQueryWrapper<MjProduct> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(MjProduct::getRouter,mjProduct.getRouter());
        List<MjProduct> mjProducts = mjProductDao.selectList(queryWrapper1);
        if(mjProducts!=null&&mjProducts.size()>0)
        {
            if(mjProducts.get(0).getId()!=mjProduct.getId())
            {
                return null;
            }
        }
        LambdaQueryWrapper<MjProduct> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MjProduct::getId,mjProduct.getId());
        this.mjProductDao.update(mjProduct,queryWrapper);
        return this.queryById(mjProduct.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.mjProductDao.deleteById(id) > 0;
    }
}
